package xyz.utils;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;

import xyz.application.MyApplication;
import xyz.receiver.AlarmReceiver;
import xyz.test.ShowNoteActivity;

public class Util {
    public static void setNoteAlarm(Context context, int noteId, long time) {
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("noteId", noteId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, noteId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (manager != null) {
            manager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
        }


    }

    public static void sendNotification(Context context, int id) {
        Intent intent = new Intent(context, ShowNoteActivity.class);
        intent.putExtra("id", id);

        TaskStackBuilder builder = TaskStackBuilder.create(context);
        builder.addNextIntentWithParentStack(intent);

        PendingIntent pendingIntent = builder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context, MyApplication.CHANNEL_ID);
        builder1.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
                .setContentTitle("Note Alarm")
                .setContentText("This is a note alarm")
                .setContentIntent(pendingIntent)
        ;

        Notification notification = builder1.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (manager != null) {
            manager.notify(id, notification);
        }

    }
}
